import { Model, DataTypes, Op } from 'sequelize';
import db from '../db/mysql';

class AppointTask extends Model {
  id: number;
  appointDate: string;
  appointTime: number;
  status: number;
  touristInfo: string;
  touristNum: number;
  platform: number;
  failreason: string;
}

AppointTask.init(
  {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    appointDate: {
      type: DataTypes.STRING(100),
      defaultValue: '',
      comment: '预约日期',
    },
    appointTime: {
      type: DataTypes.TINYINT({ length: 1 }),
      comment: '预约时段',
    },
    status: {
      type: DataTypes.TINYINT({ length: 1 }),
      defaultValue: 1,
      comment: '状态',
    },
    touristInfo: {
      type: DataTypes.STRING(100),
      defaultValue: '',
      comment: '游客信息',
    },
    touristNum: {
      type: DataTypes.TINYINT({ length: 1 }),
      defaultValue: 1,
      comment: '游客人数',
    },
    platform: {
      type: DataTypes.TINYINT({ length: 1 }),
      defaultValue: 1,
      comment: '平台',
    },
    failreason: {
      type: DataTypes.STRING(500),
      defaultValue: 1,
      comment: '失败原因',
    },
  },
  {
    sequelize: db,
    freezeTableName: true,
    tableName: 't_appoint_task',
    indexes: [
      {
        fields: ['id'],
      },
      {
        fields: ['id', 'platform'],
      },
    ],
  }
);

//强制初始化数据库
// AppointTask.sync({ force: true });

export default {
  sync(force = true) {
    return AppointTask.sync({ force });
  },
  insert: function (model: any) {
    return AppointTask.create(model);
  },
  update(data, id) {
    return AppointTask.update(data, { where: { id } });
  },
  findOne(id: number) {
    return AppointTask.findOne({
      where: { id },
      attributes: ['id', 'platform', 'appointDate', 'appointTime', 'status', 'touristInfo'],
    });
  },
  findTask(appointDate: string, platform: number) {
    return AppointTask.findOne({
      where: { appointDate, platform, status: 1 },
      attributes: ['id', 'platform', 'appointDate', 'appointTime', 'status', 'touristInfo'],
    });
  },
  getAppointList(platform: number) {
    return AppointTask.findAll({
      where: { platform, status: 1 },
      attributes: ['appointDate', 'appointTime'],
      group: ['appointDate', 'appointTime']
    });
  },
  getAppointTaskList(platform: number, appointDate: string) {
    return AppointTask.findAll({
      where: { platform, status: 1, appointDate },
      attributes: ['appointDate', 'appointTime'],
    });
  },
  getlist(pageindex: number, platform?: number) {
    let opts: any = {};
    if (platform) {
      opts.platform = platform;
    }

    opts.status = {
      [Op.in]: [1, 2],
    };
    return AppointTask.findAndCountAll({
      where: opts,
      order: [['appointDate', 'desc']],
      limit: 20,
      offset: (pageindex - 1) * 20,
      attributes: ['id', 'platform', 'appointDate', 'appointTime', 'status', 'touristInfo'],
    });
  },
  delList(ids: string[]) {
    return AppointTask.destroy({
      where: {
        id: {
          [Op.in]: ids,
        },
      },
    });
  },
};
